package longrise.phone.com.bjjt_henan.Utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.provider.Settings;
import android.view.View;

import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

import longrise.phone.com.bjjt_henan.MainActivity;
import longrise.phone.com.bjjt_henan.R;

/**
 * Created by Administrator on 2016/7/16.
 */
public class BDLocationManager implements MainActivity.OnActivityReturnBitmap {

    private Context context;

    private LocationClient client;

    private BDLocationListener listener;

    public BDLocationManager(Context context) {
        this.context = context;
        if (context instanceof MainActivity) {
            ((MainActivity) context).setOnActivityReturnBitmap(this);
        }
    }

    public BDLocationManager(Context context, boolean isReturn) {
        this.context = context;
    }

    //开始定位获取经纬度
    public void startLocation() {
        if (checkGPS()) {
            bdLocation();
        }
    }

    //检测GPS功能是否开启
    private boolean checkGPS() {
        LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            showGPSDialog();
            return false;
        }
        return true;
    }

    //显示提示对话框
    private void showGPSDialog() {
        UiUtils.showDialog(context, R.mipmap.warn, null, "GPS定位功能未开启,是否打开？", "取消", "确定",
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bdLocation();
                    }
                }, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        if (context instanceof MainActivity) {
                            ((MainActivity) context).startActivityForResult(intent, 66);
                        } else {
                            ((Activity) context).startActivityForResult(intent, 66);
                        }
                    }
                });
    }

    //启动百度定位获取经纬度
    public void bdLocation() {
        if (client == null) {
            LocationClientOption option = new LocationClientOption();
            option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);// 可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
            option.setCoorType("bd09ll");
            option.setScanSpan(10000);// 可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
            option.setIsNeedAddress(true);// 可选，设置是否需要地址信息，默认不需要
            option.setOpenGps(true);// 可选，默认false,设置是否使用gps
            option.setLocationNotify(true);// 可选，默认false，设置是否当gps有效时按照1S1次频率输出GPS结果
            option.setIgnoreKillProcess(true);// 可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死
            option.setAddrType("all"); // 设置返回类型，all表示返回所有的数据
            client = new LocationClient(context, option);
            client.registerLocationListener(listener);
        }
        // 开始定位
        if (null != client && !client.isStarted()) {
            client.start();
        }
    }

    @Override
    public void activityReturnBitmap(int requestCode, int resultCode, Intent data) {
        if (requestCode == 66) {
            bdLocation();
        }
    }

    public void stop() {
        if (null != client && client.isStarted()) {
            client.stop();
        }
    }

    public void ondestroty() {
        if (client != null) {
            stop();
            client.unRegisterLocationListener(listener);
            client = null;
        }
    }

    public void setLocationListener(BDLocationListener listener) {
        this.listener = listener;
    }
}
